import 'package:netstorage/model/CommonModel.dart';
import 'package:netstorage/model/grid_nav.dart';
import 'package:netstorage/model/home_config.dart';
import 'package:netstorage/model/sales_box.dart';

class HomeModel {
  HomeConfig? config;
  List<CommonModel>? bannerList;
  List<CommonModel>? localNavList;
  GridNav? gridNav;
  List<CommonModel>? subNavList;
  SalesBox? salesBox;

  HomeModel({this.config,this.bannerList,this.localNavList,this.gridNav,this.subNavList,this.salesBox});
  factory HomeModel.fromJson(Map<String,dynamic> map){
    //#region config
    Map<String,dynamic> configMap = map['config'] as Map<String,dynamic>;
    HomeConfig config = HomeConfig.fromJson(configMap);
    //#endregion

    //#region bannerList
    List<Map<String,dynamic>> bannerMapList = (map['bannerList'] as List?)?.cast<Map<String,dynamic>>() ?? [];
    List<CommonModel> bannerList = [];
    for(var item in bannerMapList){
      CommonModel banner = CommonModel.fromJson(item);
      bannerList.add(banner!);
    }
    //#endregion

    //#region localNavList
    List<Map<String,dynamic>> localNavMapList = (map['localNavList'] as List?)?.cast<Map<String,dynamic>>() ?? [];
    List<CommonModel> localNavList = [];
    for(var item in localNavMapList){
      CommonModel localNav = CommonModel.fromJson(item);
      localNavList.add(localNav);
    }
    //#endregion

    Map<String,dynamic> gridNavMap = map['gridNav'] ?? {};
    GridNav gridNav = GridNav.fromJson(gridNavMap);

    List<Map<String,dynamic>> subNavMapList = (map['subNavList'] as List?)?.cast<Map<String,dynamic>>() ?? [];
    List<CommonModel> subNavList = [];
    for(var item in subNavMapList){
      CommonModel subNav = CommonModel.fromJson(item);
      subNavList.add(subNav);
    }

    Map<String,dynamic> salesBoxMap = map['salesBox'] ?? {};
    SalesBox salesBox = SalesBox.fromJson(salesBoxMap);

    return HomeModel(
        config:config,
        bannerList:bannerList,
        localNavList:localNavList,
        gridNav:gridNav,
        subNavList:subNavList,
        salesBox:salesBox);
  }
  factory HomeModel.empty(){
    return HomeModel();
  }

  Map<String,dynamic> toJson(){
    Map<String,dynamic> ret = {};
    ret['config'] = config;
    ret['bannerList'] = bannerList;
    ret['localNavList'] = localNavList;
    ret['gridNav'] = gridNav;
    ret['subNavList'] = subNavList;
    ret['salesBox'] = salesBox;
    return ret;
  }

  @override
  String toString() {
    return 'config:${config.toString()}, bannerList:${bannerList.toString()}, localNavList:${localNavList.toString()}, gridNav:${gridNav.toString()}, subNavList:${subNavList.toString()}, salesBox:${salesBox.toString()}';
  }
}